define(function(require, exports, module) {
    var layer = require("js/plugins/layer/layer.js");
    var Q = require("Q");
    var Ajax = {
        /**
         * 基础ajax提交
         * @options 类型：object, ajax的传参对象
         * */
        ajax: function (option) {
            var t = this;
            //默认参数
            var data = {
                type: "post",
                dataType: "json",
            }
            $.extend(data, option);
            //重写success，如果传回的data.returnCode值为10000,才产生回调，供模块使用  999994 为登录超时  logoOut.jsp
            data.success = function (obj) {
                if (data.dataType == "html") {
                    obj = JSON.parse(obj);
                }
                if (obj.returnCode==10000) {
                    typeof option.success == "function" && option.success(obj);
                }else{
                	//访问未授权
/*                    if (obj.resultCode && obj.resultCode == -51) {
                        layer.alert(obj.resultDesc,{title:"温馨提示"},function(){
                        	window.location.href =  localhost+ "/views/index.jsp#!index/login";
                        });
                        return false;
                    }*/
                    //如果登陆会话已过期，跳到登录页
                    if (obj.returnCode == 10009) {
                        t.showLoginBox();
                        return false;
                    }else if (obj.returnCode == 10008) { //跳转登录页
                    	layer.msg(obj.returnMessage,{time: 1000},function(){});
            			if(!(window.location.href.indexOf("login")>-1)){
                        	setTimeout(function () {
                                window.location.href = localhost + "/views/index.jsp#!index/login";
                             }, 1000);
                        }
                        return false;
                    }else if (obj.returnCode == 10006) { //单设备登陆 -- 跳转登录页
                    	layer.msg(obj.returnMessage,{time: 2000},function(){
                    		window.location.href = localhost + "/views/index.jsp#!index/login";
                    	});
                        return false;
                    }else if (obj.returnCode == 10003) { //操作异常
                    	layer.msg(obj.returnMessage);
                    	setTimeout(function () {
                            $(".loadingBox").remove();
                         }, 3000);
                        return false;
                    } else if (obj.returnCode == 10010) { //允许登陆
                    	
                    } else {  //用户名或密码错误(10001),无访问权限(10007)
                    	layer.msg(obj.returnMessage);
                    }
                }
            }
            return $.ajax(data);
        },
        /**
         * session过期后的弹出登陆框
         * */
        showLoginBox: function () {
            layer.msg('登录超时!');
            setTimeout(function () {
               window.location.href = localhost + "/views/index.jsp#!index/login";
            }, 2000);
        }
    };
    module.exports = Ajax;
});